﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class PaymentsList : TAM_SPORT.FormBase
    {
        private PaymentDao pay;
        public PaymentsList()
        {
            InitializeComponent();
            pay = new PaymentDao();
        }

        void intit()
        {
            dtgCustomers.Rows.Clear();
            DataTable table = pay.getPayments(dtFromDate.Value, dtpToDate.Value);
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[1].Value = row1["CREATE_AT"].GetType().FullName == "System.DBNull" ? "" : DateTime.Parse(row1["CREATE_AT"].ToString()).ToString("dd/MM/yyyy");
                    dtgCustomers.Rows[i].Cells[2].Value = row1["REASON"].ToString();
                    dtgCustomers.Rows[i].Cells[3].Value = row1["TOTAL"].GetType().FullName == "System.DBNull" ? "0" : float.Parse(row1["TOTAL"].ToString()).ToString("#,###");
                    dtgCustomers.Rows[i].Cells[4].Value = row1["PERSON_RECEIVE"].ToString();
                    dtgCustomers.Rows[i].Cells[6].Value = row1["NOTES"].ToString();
                    dtgCustomers.Rows[i].Cells[6].Value = row1["ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
            DataRow ro = pay.getTotalPayment(dtFromDate.Value, dtpToDate.Value);
            if (ro != null)
            {
                lblTongSoTienChi.Text = ro["TOTAL"].GetType().FullName == "System.DBNull" ? "0" : float.Parse(ro["TOTAL"].ToString()).ToString("#,###");
            }
        }
        private void btnView_Click(object sender, EventArgs e)
        {
            intit();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void dtFromDate_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dtpToDate_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dtgCustomers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void lblTongSoTienChi_Click(object sender, EventArgs e)
        {

        }

        private void dtgCustomers_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 7)
            {
                Confirm con = new Confirm(UserConstant.PAYMENT_CANCEL);
                con.ShowDialog();
                if (con.IsAccept)
                {
                    int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[6].Value.ToString());
                    if (pay.cancelPayments(id))
                    {
                        Result re = new Result(UserConstant.PAYMENT_CAN_SUCC);
                        re.ShowDialog();
                        re.Dispose();
                        intit();
                    }
                    else
                    {
                        Error er = new Error(UserConstant.PAYMENT_CAN_FAIL);
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
                con.Dispose();
            }
        }
    }
}
